/*
 * LinearAdc.cpp
 *
 * Copyright 2012 Pieter Agten
 *
 * This file is part of tltgen.
 *
 * tltgen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * tltgen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with tltgen.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file   LinearAdc.cpp
 * @author Pieter Agten (pieter.agten@gmail.com)
 * @date   5 mar 2012
 */


#include <stdexcept>
#include <math.h>
#include <boost/lexical_cast.hpp>

#include "AdcVisitor.hpp"

#include "LinearAdc.hpp"

namespace tltgen {

  LinearAdc::LinearAdc(unsigned int nbBits, Voltage reference) :
    nbBits(nbBits), reference(reference)
  {
    if (! isValidNbBits(nbBits)) {
      throw std::invalid_argument("Value '"+ boost::lexical_cast<std::string>(nbBits) +
				  "' is not a valid maximum ADC output value.");
    }
    if (! isValidReferenceVoltage(reference)) {
      throw std::invalid_argument("Value '"+ boost::lexical_cast<std::string>(reference) +
				  "' is not a valid ADC reference voltage.");
    }
  }

  LinearAdc::~LinearAdc()
  { }


  bool LinearAdc::isValidNbBits(unsigned int nbBits) {
    return nbBits < 32;
  }

  bool LinearAdc::isValidReferenceVoltage(Voltage reference) {
    return std::isfinite(reference) && reference > 0;
  }

  unsigned int LinearAdc::getNbBits() const {
    return this->nbBits;
  }

  Voltage LinearAdc::getReferenceVoltage() const {
    return this->reference;
  }

  AdcValue LinearAdc::getMinimumOutputValue() const {
    return 0;
  }

  AdcValue LinearAdc::getMaximumOutputValue() const {
    return (1 << this->nbBits) - 1;
  }


  bool LinearAdc::isValidOutputValue(AdcValue adcValue) const {
    return getMinimumOutputValue() <= adcValue && 
      adcValue <= getMaximumOutputValue();
  }

  Voltage LinearAdc::getInputVoltage(AdcValue adcValue) const {
    if (! isValidOutputValue(adcValue)) {
      throw std::invalid_argument("Value '"+ boost::lexical_cast<std::string>(adcValue) +
				  "' is not a valid ADC output value.");
    }

    AdcValue adcMin = this->getMinimumOutputValue();
    AdcValue adcMax = this->getMaximumOutputValue();
    Voltage Vref = this->getReferenceVoltage();

    Voltage input = (adcValue - adcMin) * Vref/(adcMax - adcMin);
    return input;
  }


  bool LinearAdc::isValidInputVoltage(Voltage voltage) const {
    return isValidVoltage(voltage) && voltage >= 0;
  }

  AdcValue LinearAdc::getOutputValue(Voltage V) const {
    if (! isValidInputVoltage(V)) {
      throw std::invalid_argument("Value '"+ boost::lexical_cast<std::string>(V) +
				  "' is not a valid ADC input voltage.");
    }
    AdcValue ADCmin = this->getMinimumOutputValue();
    AdcValue ADCmax = this->getMaximumOutputValue();
    Voltage Vref = this->getReferenceVoltage();
    if (V > Vref) {
      return ADCmax;
    }

    AdcValue output = floor(ADCmin + (V/Vref * (ADCmax - ADCmin)) + 0.5);
    return output;
  }

  void LinearAdc::accept(AdcVisitor* visitor) const
  {
    visitor->visit(this);
  }

}

BOOST_CLASS_EXPORT_IMPLEMENT(tltgen::LinearAdc)

